package uestc.lj.midPromotion.tree.prefixtree;

import java.util.TreeMap;

/**
 * @Author:Crazlee
 * @Date:2021/12/2
 */
public class Code01_GetFolderTree {
	public static class Node {
		public String name;
		public TreeMap<String, Node> nextMap;

		public Node(String name) {
			this.name = name;
			nextMap = new TreeMap<>();
		}
	}

	public static void print(String[] folderPaths) {
		if (folderPaths == null || folderPaths.length == 0) {
			return;
		}
		Node head = generateFolderTree(folderPaths);
		printProcess(head, 0);
	}

	private static Node generateFolderTree(String[] folderPaths) {
		Node head = new Node("");
		for (String folderPath : folderPaths) {
			String[] paths = folderPath.split("\\\\");
			Node cur = head;
			for (String path : paths) {
				if (!cur.nextMap.containsKey(path)) {
					cur.nextMap.put(path, new Node(path));
				}
				cur = cur.nextMap.get(path);
			}
		}
		return head;
	}

	private static void printProcess(Node head, int level) {
		if (level != 0) {
			System.out.println(get2nSpace(level) + head.name);
		}
		for (Node next : head.nextMap.values()) {
			printProcess(next, level + 1);
		}
	}

	private static String get2nSpace(int n) {
		String result = "";
		for (int i = 1; i < n; i++) {
			result += "  ";
		}
		return result;
	}

	public static void main(String[] args) {
		String[] arr = {"b\\cst", "d\\", "a\\d\\e", "a\\b\\c"};
		print(arr);
	}
}
